var cartStr = getCookie('cart');
//判断购物车是否为空
if(cartStr){
	$('.empty').hide();
	$('tfoot').show();
	
	var cart = JSON.parse(cartStr);	
	var sum = 0;//全部商品总价
	var numberG = 0;//全部商品个数
	//根据商品创建tr元素
	for(var i in cart){
		var good = cart[i];		
		var oTr = '<tr id="'+i+'"><td align="left"><p><img src="'+good.urlImg+'"width="55"style="vertical-align:middle;margin-right:5px;"><a href="#">'+good.name+'</a></td>';
		oTr += '<td><span class="special">￥'+good.price1+'</span></td>';
		oTr += '<td><b>￥'+good.price+'</b></td>';
		oTr += '<td><a href="javascript:;"></a><input size="3" type="text" id="numA" value="'+good.num+'"><a href="javascript:;"></a></td>';
		oTr += '<td><strong>￥'+(good.price*good.num)+'</strong></td>';
		oTr += '<td class="blue"><a href="javascript:;">取消订购</a></td></tr>';
		$('tbody').append(oTr);
		sum += good.price*good.num;
		numberG += good.num;
	}
	//更新商品总价和商品总个数
	$('tfoot').find('#things_number').html(numberG);
	$('tfoot').find('#things_price').html(sum)
	
}else{
	$('.empty').show();
	$('tfoot').hide();
}
//点击取消商品
$('.blue a').click(function(){
	//alert(1)
	var goodId = $(this).parent().parent().attr('id');
	//console.log(goodId)
	var cartStr = getCookie('cart');
	var cart = JSON.parse(cartStr);
	//更新商品的总价和个数
	sum -= cart[goodId].price*cart[goodId].num;
	numberG -= cart[goodId].num;
	
	deleteGood(goodId);
	
    var str = '#'+goodId;
 	$(this).parent().parent().remove();	
	$('tfoot').find('#things_number').html(numberG);
	$('tfoot').find('#things_price').html(sum);
	
	judge();
})
//点击清空商品
$('#clearAll').click(function(){
	$('.empty').show();
	$('tfoot').hide();
	removeCookie('cart');
	$('tbody').remove();
	
	
//	var s = $('tbody tr').length;
//	for(var i=0;i<s;i++){
//		var goodId = $('tbody tr').eq(i).attr('id');
//		deleteGood(goodId);
//	}
//	
//	$('tbody').remove();	
})
//删除商品
function deleteGood(goodId){
	var cartStr = getCookie('cart');
	var cart = JSON.parse(cartStr);
	delete cart[goodId];
	var res = cart;
	var resStr = JSON.stringify(res);
	setCookie('cart', resStr, 7);
}
//判断商品是否为空
function judge(){
	var cartStr = getCookie('cart');	
	if(cartStr!='{}'){
		$('.empty').hide();
		$('tfoot').show();
	}else{		
		$('.empty').show();
		$('tfoot').hide();
	}
}

//点击结算
$('#buyId').click(function(){
	location.href = 'settlementPage.html';
})
